﻿package core.dialog{
	
	import flash.display.MovieClip;
	import base.BaseMc;
	import flash.events.MouseEvent;
	import core.net.socket.cmd.BluetoothUnBindCmd;
	import core.net.socket.cmd.BluetoothConnectCmd;
	import core.events.CmdEvent;
	import core.User;
	import core.events.MessageDialogEvent;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import core.events.DialogViewBluetoothEvent;
	import core.net.socket.cmd.BluetoothDisConnectCmd;
	

	public class DialogViewBluetoothConnectFailed extends BaseMc {
		
		private var timer:Timer;
		
		public function DialogViewBluetoothConnectFailed() {
			
		} 
		protected override function onInit():void { 
			this.contentText.text = "请先检查魔法棒是否开启，5秒后自动重连";
			this.btn_lianjie.addEventListener(MouseEvent.CLICK,onLianjieClick);
			this.btn_jiebang.addEventListener(MouseEvent.CLICK,onJiebangClick);
			this.centerOfParent();
			this.y=150;
			
		}
		
		public override function show():void {
			super.show();
			timer = new Timer(5000,1);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
		}
		
		
		private function onTimer(e:TimerEvent):void {
			if(this.parent != null){
				//this.sendCmd(BluetoothDisConnectCmd.make());
				this.dispatchEvent(new DialogViewBluetoothEvent(DialogViewBluetoothEvent.CONNECTING));
			}
		}
		protected override function onRemove():void {
			this.btn_jiebang.removeEventListener(MouseEvent.CLICK,onJiebangClick);
			if(timer){
				timer.removeEventListener(TimerEvent.TIMER,onTimer);
			}
		}
		
		
		public function onJiebangClick(e:MouseEvent):void { 
			if(timer){
				timer.removeEventListener(TimerEvent.TIMER,onTimer);
			}
			var dlg:DialogMessage = new DialogMessage("确定解绑吗？",false);
			dlg.addEventListener(MessageDialogEvent.OK,onJiebangByUser);
			this.stage.addChild(dlg);
		}
		
		
		private function onJiebangByUser(e:MessageDialogEvent):void {
			this.sendCmd(BluetoothUnBindCmd.make());
			User.deviceAddress = "";
			DialogBluetooth.connected = false; 
			var p:BaseMc = this.parent as BaseMc;
			 p.remove();
		}
		
		
		private function onLianjieClick(e:MouseEvent):void {
			this.dispatchEvent(new DialogViewBluetoothEvent(DialogViewBluetoothEvent.CONNECTING));
		}
	}
	
}
